package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mta.pokerstats.Game;
import edu.mta.pokerstats.GameRecord;

/**
 * Servlet implementation class servletGameRecord
 */
@SuppressWarnings("static-method")
public class servletGameRecord extends BaseRecordsAbstractServlet {
	private static final long serialVersionUID = 1L;

	private static final String k_BalancePrefix = "\"balance\" : ";
	private static final String k_InvestedPrefix = "\"invested\" : ";

	protected void processInsertRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		GameRecord gameRecord;
		int gameID = Integer.parseInt(request.getParameter("gameID"));
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		int numOfRebuys;
		int winnings;
		int position;
		int knockouts;
		int investment;
		int balance;
		
		numOfRebuys = Integer.parseInt(request.getParameter("numOfRebuys"));
		winnings = Integer.parseInt(request.getParameter("winnings"));
		position = Integer.parseInt(request.getParameter("position"));
		knockouts = Integer.parseInt(request.getParameter("knockouts"));
		investment = Game.getGameById(gameID).GetInvestment(numOfRebuys);
		balance = winnings - investment;
		gameRecord = new GameRecord(gameID, playerID, numOfRebuys,
				winnings, position, knockouts);
		if (gameRecord.InsertToDatabase()) {
			out.print("{" + k_SuccessStr + ","
					+ k_BalancePrefix + "\""+ balance +"\","
					+ k_InvestedPrefix + "\"" + investment + "\"}");
		} else {
			out.print("{"+k_FailStr+"}");
		}
	}

	protected void processUpdateRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		GameRecord gameRecord;
		int gameID = Integer.parseInt(request.getParameter("gameID"));
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		int numOfRebuys;
		int winnings;
		int position;
		int knockouts;
		int investment;
		int balance;
		
		gameRecord = GameRecord.getGameRecordByIds(gameID, playerID);
		numOfRebuys = Integer.parseInt(request.getParameter("numOfRebuys"));
		gameRecord.setNumberOfRebuys(numOfRebuys);
		winnings = Integer.parseInt(request.getParameter("winnings"));
		gameRecord.setWonInGame(winnings);
		position = Integer.parseInt(request.getParameter("position"));
		gameRecord.setPosition(position);
		knockouts = Integer.parseInt(request.getParameter("knockouts"));
		gameRecord.setKnockouts(knockouts);
		investment = Game.getGameById(gameID).GetInvestment(numOfRebuys);
		balance = winnings - investment;
		if (gameRecord.UpdateInDatabase()) {
			out.print("{" + k_SuccessStr + ","
					+ k_BalancePrefix + "\""+ balance +"\","
					+ k_InvestedPrefix + "\"" + investment + "\"}");
		} else {
			out.print("{"+k_FailStr+"}");
		}	
	}

	protected void processDeleteRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		GameRecord gameRecord;
		int gameID = Integer.parseInt(request.getParameter("gameID"));
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		
		gameRecord = GameRecord.getGameRecordByIds(gameID, playerID);
		if (gameRecord.DeleteFromDatabase()) {
			out.print("{"+k_SuccessStr+"}");
		} else {
			out.print("{"+k_FailStr+"}");
		}
	}
}